﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RealEstateBrowser.ViewModels;
using RealEstateAgencyDAL;

namespace RealEstateBrowser.Helpers
{
    public static class Skochubra
    {
        private static DistrictViewModel CreateDistrictViewModeFromDistrict(District district)
        {
            DistrictViewModel districtVM = new DistrictViewModel();
            districtVM.District = district;

            return districtVM;
        }

        public static IEnumerable<DistrictViewModel> CreateDisrictViewModelCollection(IEnumerable<Location> locations)
        {
            List<DistrictViewModel> districtVMs = new List<DistrictViewModel>();

            foreach (Location location in locations)
            {
                districtVMs.Add(CreateDistrictViewModeFromDistrict(location.District));
            }

            return districtVMs;
        }

        internal static IEnumerable<TownViewModel> GetAllTowns()
        {
            var towns = RealEstateDAL.GetAllTowns();
            List<TownViewModel> townVMs = new List<TownViewModel>();

            foreach (Town town in towns)
            {
                townVMs.Add(CreateTownVieModelFromTown(town));
            }

            return townVMs;
        }

        private static TownViewModel CreateTownVieModelFromTown(Town town)
        {
            TownViewModel townVM = new TownViewModel();
            townVM.Town = town;
            return townVM;
        }

        internal static IEnumerable<OfferViewModel> GetOffers(string town, string district)
        {
            var offers = RealEstateDAL.GetAllOffers();
            var filtered = (from o in offers
                            where o.Location.District.DistrictName == district && o.Location.Town.TownName == town
                            select new OfferViewModel() { Offer = o }).ToList();

            return filtered;
        }

        internal static IEnumerable<OfferTypeViewModel> GetOfferTypes()
        {
            var offerTypes = RealEstateDAL.GetAllOfferTypes();
            List<OfferTypeViewModel> offerTypeVMs = new List<OfferTypeViewModel>();

            foreach (OfferType ot in offerTypes)
            {
                offerTypeVMs.Add(CreateOfferTypeViewModel(ot));
            }

            return offerTypeVMs;
        }

        private static OfferTypeViewModel CreateOfferTypeViewModel(OfferType offerType)
        {
            return new OfferTypeViewModel() { OfferType = offerType };
        }

        internal static IEnumerable<ExtraViewModel> GetExtras()
        {
            var extras = RealEstateDAL.GetAllExtras();
            List<ExtraViewModel> extraVMs = new List<ExtraViewModel>();

            foreach (var extra in extras) 
            {
                extraVMs.Add(CreateExtraViewModel(extra));
            }

            return extraVMs;
        }
  
        private static ExtraViewModel CreateExtraViewModel(Extra extra)
        {
            return new ExtraViewModel() { Extra = extra };
        }

        internal static IEnumerable<EstateTypeViewModel> GetEstateTypes()
        {
            var estateTypes = RealEstateDAL.GetAllEstateTypes();
            List<EstateTypeViewModel> estateTypeVMs = new List<EstateTypeViewModel>();

            foreach (var estateType in estateTypes)
            {
                estateTypeVMs.Add(CreateEstateTypeViewModel(estateType));
            }

            return estateTypeVMs;
        }
  
        private static EstateTypeViewModel CreateEstateTypeViewModel(EstateType estateType)
        {
            return new EstateTypeViewModel() { EstateType = estateType };
        }



        internal static IEnumerable<OfferViewModel> GetAllOffers()
        {
            var offers = RealEstateDAL.GetAllOffers();
            List<OfferViewModel> offersVMs = new List<OfferViewModel>() ;

            foreach (var offer in offers)
            {
                offersVMs.Add(new OfferViewModel() { Offer = offer }); 
            }

            return offersVMs;
        }

        public static IEnumerable<ExtraViewModel> GetAllExtras()
        {
            var extras = RealEstateDAL.GetAllExtras();
            List<ExtraViewModel> extaVMs = new List<ExtraViewModel>();

            foreach (var extra in extras)
            {
                extaVMs.Add(new ExtraViewModel() { Extra = extra, IsChecked = false });
            }

            return extaVMs;
        }
    }
}